package com.cskaoyan.controller.wxcontroller;

import com.cskaoyan.bean.pojo.User;
import com.cskaoyan.bean.vo.BaseRespVo;
import com.cskaoyan.bean.wxbo.AddOrDeleteBO;
import com.cskaoyan.bean.wxbo.WxCollectBO;
import com.cskaoyan.bean.wxvo.AddOrDeleteCollectVO;
import com.cskaoyan.bean.wxvo.WxCollectVO;
import com.cskaoyan.service.UserService;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;


/**
 * @Author Gan
 * @Date 2021/7/12 9:11
 * @Description:
 */
@RestController
public class WxCollectController {

    @Autowired
    UserService userService;

    @RequestMapping("wx/collect/list")
    public BaseRespVo collectList(WxCollectBO wxCollectBO) {
        Subject subject = SecurityUtils.getSubject();
        User user = (User) subject.getPrincipals().getPrimaryPrincipal();
        if (user.getId() == null){
            return BaseRespVo.fail("请先登录！");
        }
        WxCollectVO wxCollectVO = userService.collectList(user.getId(), wxCollectBO);
        return BaseRespVo.ok(wxCollectVO);

//        int valueId = userService.selectValueIdByUserId(user.getId());
//        int type = userService.selectTypeByUserId(user.getId());
//        int id = userService.selectIdByUserId(user.getId());
//        String name = userService.selectNameById(valueId);
//        String brief = userService.selectBriefById(valueId);
//        String picUrl = userService.selectPicUrlById(valueId);
//        BigDecimal retailPrice = userService.selectRetailPriceById(valueId);
//        CollectList collectList = new CollectList();
//        collectList.setType(type);
//        collectList.setValueId(valueId);
//        collectList.setId(id);
//        collectList.setName(name);
//        collectList.setBrief(brief);
//        collectList.setPicUrl(picUrl);
//        collectList.setRetailPrice(retailPrice);
//        wxCollectVO.setTotalPages(1);
//        wxCollectVO.setCollectList(collectList);
//        return BaseRespVo.ok(wxCollectVO);*/
    }

    @PostMapping("wx/collect/addordelete")
    public BaseRespVo addOrDelete(@RequestBody AddOrDeleteBO addOrDeleteBO){

        Subject subject = SecurityUtils.getSubject();
        User user = (User) subject.getPrincipals().getPrimaryPrincipal();
        if (user.getUsername() == null){
            return BaseRespVo.fail("请先登录！");
        }
        AddOrDeleteCollectVO addOrDeleteCollectVO = new AddOrDeleteCollectVO();
        int i = userService.addOrDelete(addOrDeleteBO.getType(), addOrDeleteBO.getValueId(), user.getId());
        if (i > 0){
            //delete
            addOrDeleteCollectVO.setType("delete");
        }else {
            //add
            addOrDeleteCollectVO.setType("add");
        }

        return BaseRespVo.ok(addOrDeleteCollectVO);
    }
}
